<template>
  <div class="table-box">
    <!-- <div class="table-search">
      <div class="btn-box">
        <el-button type="primary" @click="onAdd">新增</el-button>
        <el-button type="primary" plain @click="onExport">导出</el-button>
      </div>
    </div>-->
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="商户id">
          <el-input v-model="form.mer_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item label="渠道ID">
          <el-input v-model="form.channel_id" placeholder="渠道ID" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" @click="onAdd">新增</el-button>
          <!-- <el-button type="primary" plain @click="onExport">导出</el-button> -->
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="id" align="center" />
          <el-table-column prop="merchant_id" label="商户id" align="center" />
          <el-table-column
            prop="channel_id"
            label="支付通道id"
            align="center"
          />
          <el-table-column prop="name" label="通道名称" align="center" />
          <el-table-column prop="currency" label="支付币种" align="center" />
          <el-table-column prop="fee_rate" label="通道费率" align="center" />
          <el-table-column
            prop="fixed_fee"
            label="单笔固定费用"
            align="center"
          />
          <el-table-column
            prop="fixed_usd_fee"
            label="单笔美元固定费用"
            align="center"
          />
          <el-table-column prop="diff_rate" label="汇差费率" align="center" />
          <el-table-column
            prop="deposit_freeze_days"
            label="入账天数"
            align="center"
          />
          <el-table-column
            prop="min_amount"
            label="最小支付金额"
            align="center"
          />
          <el-table-column
            prop="max_amount"
            label="最大支付金额"
            align="center"
          />
          <el-table-column
            prop="daily_max_amount"
            label="单日最大限额"
            align="center"
          />
          <el-table-column
            prop="is_valid"
            label="是否生效"
            align="center"
            width="center"
          >
            <template #default="scope">
              <el-tag :type="scope.row.is_valid ? 'success' : 'danger'">{{
                scope.row.is_valid ? "已生效" : "未生效"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="is_refund"
            label="是否允许退款"
            align="center"
            width="center"
          >
            <template #default="scope">
              <el-tag :type="scope.row.is_refund ? 'success' : 'danger'">{{
                scope.row.is_refund ? "是" : "否"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right">
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      @handleUploaderImgRequest="handleUploader"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :supplierList="supplierList"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import {
  merPayListApi,
  merPayAddApi,
  merPayUpdateApi,
} from "@/api/ExAssistant/merchant";
import DialogForm from "./components/dialogForm.vue";
const loading = ref(false);
const tableData = ref([]);
const dialogVisible = ref(false);
const btnLoading = ref(false);
const btnType = ref(null);
const supplierList = ref([]); //游戏商
const formData = {
  mer_id: "",
  channel_id: "",
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const dialogFormData = {
  id: "",
  merchant_id: "",
  channel_id: "",
  currency: "",
  fee_rate: "",
  fixed_fee: "",
  fixed_usd_fee: "",
  diff_rate: "",
  deposit_freeze_days: "",
  min_amount: "",
  max_amount: "",
  daily_max_amount: "",
  is_valid: "",
  is_refund: "",
  createtime: "",
};
const dialogForm = reactive({ ...dialogFormData });
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    mer_id: form.mer_id,
    channel_id: form.channel_id,
  };
};
// 获取
const getSupplierList = async () => {
  try {
    const res = await merPayListApi({});

    supplierList.value = res.data;
  } catch (error) {
    supplierList.value = [];
  }
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await merPayListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 查询
const onQuery = () => {
  console.log("onQuery");
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 新增
const onAdd = async () => {
  dialogVisible.value = true;
  btnType.value = "add";
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  delete param.src;
  try {
    btnLoading.value = true;
    if (btnType.value == "add") {
      delete param.id;

      const res = await merPayAddApi(param);

      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    if (btnType.value == "edit") {
      console.log(param);
      delete param.createtime;
      const res = await merPayUpdateApi(param);

      ElMessage({
        message: res.message,
        type: "success",
      });
    }

    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};

const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    delete row.createtime;
    delete row.updatetime;
    // const { createtime, updatetime, ...data } = row;
    Object.assign(dialogForm, { ...row, src: row.icon });
    dialogForm.merchant_id = row.merchant_id;
    dialogVisible.value = true;
  }
  // 删除
  //  if (type === "delete") {
  //    ElMessageBox.confirm("您是否确认删除此商户渠道?", "温馨提示", {
  //      type: "warning"
  //    }).then(async () => {
  //      const res = await merPayDeleteApi({ id: row.id });
  //      getTbaleData();
  //      ElMessage.success(res.message);
  //    });
  //  }
};
// //上传图标
const handleUploader = (res, type) => {
  if (res.code === 0) {
    ElMessage({
      message: "图片上传成功",
      type: "success",
    });

    dialogForm.src = res.data.pic_url || "";
    dialogForm.icon = res.data.name || "";
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
  getSupplierList();
});
</script>
